package com.mes.service.yzl;


import com.mes.dao.yzl.ReturnMapper;
import com.mes.entity.hpf.SaleOutDetail;
import com.mes.entity.hpf.SaleReturnDetail;
import com.mes.entity.yzl.Condition;
import com.mes.entity.yzl.ProContactOrder;
import com.mes.entity.yzl.ReturnOrder;
import com.mes.entity.yzlExtend.ExtendProductor;
import com.mes.entity.yzlExtend.ExtendReturnOdrer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ReturnServiceImpl implements ReturnService {

    @Resource
    ReturnMapper returnMapper;

    @Override
    public long addReturn(ReturnOrder returnOrder) {
        return returnMapper.addReturn(returnOrder);
    }

    @Override
    public void addCentor(ProContactOrder proContactOrder) {
        returnMapper.addCentor(proContactOrder);
        ProContactOrder pro = returnMapper.findCusCenterProByPid(proContactOrder.getId());
        List<ExtendProductor> extendProductorList = returnMapper.customerOrderXq(pro.getCustomerOrderId());
        int num = 0;
        for(int i=0;i<extendProductorList.size();i++){
        	if(extendProductorList.get(i).getAddReturnIsSelect().equals("0")){
        		num = 1;
        	}
        }
        if(num==0){
        	returnMapper.setSelectLibOrder(proContactOrder.getId(), 0);
        }
    }

    @Override
    public List<ExtendReturnOdrer> listReturnOrder(Condition condition) {
        return returnMapper.listReturnOrder(condition);
    }

    @Override
    public List<ExtendProductor> returnOrderXq(long id) {
        return returnMapper.returnOrderXq(id);
    }

    @Override
    public ExtendReturnOdrer findReturnById(long id) {
        return returnMapper.findReturnById(id);
    }

    @Override
    public long updateReturn(ReturnOrder returnOrder) {
        return returnMapper.updateReturn(returnOrder);
    }

    @Override
    public void updateCentorRetuen(ProContactOrder proContactOrder) {
        returnMapper.updateCentorRetuen(proContactOrder);
    }

    @Override
    public int checkNumUnique(String returnNumber) {
        return returnMapper.checkNumUnique(returnNumber);
    }

    @Override
    public int returnListCount(Condition condition) {
        return returnMapper.returnListCount(condition);
    }

	@Override
	public int deleteReturn(long id) {
		// TODO Auto-generated method stub
		return returnMapper.deleteReturn(id);
	}

	@Override
	public int returnApproveds(List<Integer> list) {
		// TODO Auto-generated method stub
		return returnMapper.returnApproveds(list);
	}

	@Override
	public int returnUnApproveds(List<Integer> list) {
		// TODO Auto-generated method stub
		return returnMapper.returnUnApproveds(list);
	}

	@Override
	public List<ReturnOrder> returnOrderList() {
		// TODO Auto-generated method stub
		List<ReturnOrder> newReturnOrderList = new ArrayList<ReturnOrder>();
		
		List<ReturnOrder> returnOrderList = returnMapper.returnOrderList();
		
		for (int i = 0; i < returnOrderList.size(); i++) {
			ReturnOrder returnOrder = returnOrderList.get(i);
			List<ExtendProductor> extendProductorList = returnMapper.returnOrderXq(returnOrder.getId());
			returnOrder.setExtendProductorList(extendProductorList);
			newReturnOrderList.add(returnOrder);
		}
		return newReturnOrderList;
	}

	@Override
	public void deleteReturnXq(long returnLibId) {
		// TODO Auto-generated method stub
		List<ExtendProductor> eList = returnMapper.returnOrderXq(returnLibId);
    	for(int i=0;i<eList.size();i++){
    		returnMapper.setSelectLibOrder(eList.get(i).getPid(), 1);
    	}
		returnMapper.deleteReturnXq(returnLibId);
	}

	@Override
	public int returnsProcess(List<Integer> list) {
		// TODO Auto-generated method stub
		return returnMapper.returnsProcess(list);
	}

	@Override
	public int outCount(Condition condition) {
		// TODO Auto-generated method stub
		return returnMapper.outCount(condition);
	}

	@Override
	public List<ExtendProductor> outList(Condition condition) {
		// TODO Auto-generated method stub
		return returnMapper.outList(condition);
	}

	@Override
	public List<ExtendProductor> addOutOrderXq(long id) {
		// TODO Auto-generated method stub
		return returnMapper.addOutOrderXq(id);
	}

	@Override
	public List<SaleReturnDetail> returnDetail(long id) {
		// TODO Auto-generated method stub
		return returnMapper.returnDetail(id);
	}

	@Override
	public void addReturnDetail(SaleReturnDetail saleReturnDetail) {
		// TODO Auto-generated method stub
		returnMapper.addReturnDetail(saleReturnDetail);
	}

	@Override
	public void updateOutDetailStatus(SaleOutDetail saleOutDetail) {
		// TODO Auto-generated method stub
		returnMapper.updateOutDetailStatus(saleOutDetail);
	}

	@Override
	public void updateReturnDetail(SaleReturnDetail saleReturnDetail) {
		// TODO Auto-generated method stub
		returnMapper.updateReturnDetail(saleReturnDetail);
	}

	@Override
	public void deleteReturnDetail(long id) {
		// TODO Auto-generated method stub
		List<SaleReturnDetail> saleReturnDetails = returnMapper.returnDetail(id);
		for (int i = 0; i < saleReturnDetails.size(); i++) {
			SaleReturnDetail saleReturnDetail = saleReturnDetails.get(i);
			SaleOutDetail saleOutDetail = returnMapper.findOutDetailById(saleReturnDetail.getOutDetailId());
			saleOutDetail.setLastOutQ(saleOutDetail.getLastOutQ()+saleReturnDetail.getReturnQ());
			saleOutDetail.setAddReturnIsSelect("0");
			returnMapper.setSelectOutDetail(saleOutDetail);
		}
		returnMapper.deleteReturnDetail(id);
	}
}
